_package("alz.mui");

_import("alz.mui.ActionElement");

/**
 * 可点击的文本标签组件
 */
_class("LinkLabel", ActionElement, function(){
	//组件本身支持的事件
	this.EVENTS = [
		"activate",
		"deactivate"
	];
	this._init = function(){
		_super._init.call(this);
	};
	this.bind = function(obj, actionManager){
		this._actionManager = actionManager;
		this.init(obj);
	};
	this.init = function(obj){
		_super.init.apply(this, arguments);
		if(this._tag == "a"){
			//屏蔽带有_action属性的超链接的右键菜单
			//[TODO]更好的做法是提供自定义的右键菜单
			this._self.ondragstart = function(){return false;};
			this._self.oncontextmenu = function(ev){
				//ev = ev || window.event;
				ev.stopPropagation();
				return false;
			};
			this._self.onclick = function(){
				return false;
			};
		}
		this.addListener(this._self, "tapstart", this, "onTapStart");
		this.addListener(this._self, "tapcancel", this, "onTapCancel");
		this.addListener(this._self, "tap", this, "onTap");
		/*
		if(!runtime.isTouch){
			this._self.onclick = function(ev){
				if(ev.target != this && ev.target.tagName == "A") return;
				//ev.stopPropagation();
				//return this._ptr.onAction(ev);
				return this._ptr.dispatchAction(this, ev);
			};
			/ *
			//[TODO]应该延时处理mouseup对应的逻辑代码
			var _this = this;
			function mouseup(ev){
				_this._actionManager.getActionEngine()._taskSchedule.addTask("page_unload", this, aa_mouseup);
				return false;
			}
			* /
			runtime.addGlobalListener(this._self, "mouseup", this, function(ev){
				if(this._self.disabled) return;
				this.setState("normal");
			});
		}
		*/
	};
	this.dispose = function(){
		/*
		if(!runtime.isTouch){
			runtime.removeGlobalListener(this._self, "mouseup", this);
			this._self.onclick = null;
		}
		*/
		if(this._tag == "a"){
			this._self.onclick = null;
			this._self.ondragstart = null;
			this._self.oncontextmenu = null;
		}
		_super.dispose.apply(this);
	};
	this.setState = function(v){
		switch(v){
		case "active":
			switch(this._self.tagName){
			case "LI":
				runtime._element.addClass(this._self, "cur");
				break;
			default:
				runtime._element.addClass(this._self, "hov");
				break;
			}
			this.fireEvent({"type": "activate"});
			break;
		case "normal":
		default:
			switch(this._self.tagName){
			case "LI":
				runtime._element.removeClass(this._self, "cur");
				break;
			default:
				runtime._element.removeClass(this._self, "hov");
				break;
			}
			this.fireEvent({"type": "deactivate"});
			break;
		}
	};
	/*
	this.onTouchStart = function(ev){
		//[TODO]在超链接没有_action属性的情况下，工作机制不太合理
		if(ev.target != this._self && ev.target.tagName == "A") return;
		if(this._self.disabled) return;
		//runtime.setCapture(this._self);
		//ev.stopPropagation();
		this.setState("active");
		return false;
	};
	this.onTouchMove = function(ev){
	};
	this.onTouchEnd = function(ev){
		if(this._self.disabled) return;
		var ret;
		//ret = this.onAction(ev);
		if(runtime.isTouch){
			ret = this.dispatchAction(this._self, ev);  //使用touch事件模拟onclick
		}else{
			ret = false;
		}
		this.setState("normal");
		//runtime.releaseCapture(this);
		//ev.stopPropagation();
		return ret;
	};
	*/
	this.onTapStart = function(ev){
		//runtime.log(this._className + "::onTapStart");
		if(this._disabled) return;
		this.setState("active");
		//ev.stopPropagation();  //[TODO]影响ScrollView组件的滚动行为
	};
	this.onTapCancel = function(ev){
		//runtime.log(this._className + "::onTapCancel");
		if(this._disabled) return;
		this.setState("normal");
	};
	this.onTap = function(ev){
		//runtime.log(this._className + "::onTap");
		if(this._disabled) return;
		var ret = this.dispatchAction(this._self, ev);
		this.setState("normal");
		return ret;
	};
});